﻿using SetSailCommon;
using SetSailDal;
using SetSailModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SetSailView
{
    /// <summary>
    /// 后台审核
    /// </summary>
    public partial class Frm_HTSH : UserControl
    {
        public Frm_HTSH()
        {
            InitializeComponent();
        }
        private int pageIndex = 0;
        private void Frm_HTSH_Load(object sender, EventArgs e)
        {
            this.init();
        }

        private void init()
        {
            //设置每页条目
            this.ucPagerControl31.PageSize = Convert.ToInt32(CookieData.getInstance().ZDGL.Where(a => a.MXDM == "PAGECOUNT").FirstOrDefault().MXZ);
            var totalCount = 0;
            this.LoadList(out totalCount);
            //设置分页
            this.setPage(totalCount);
        }

        private void ucBtnExt1_BtnClick(object sender, EventArgs e)
        {
            //筛选条件
            pageIndex = 0; //清空index，防止数据变更触发分页事件
            this.ucPagerControl31.FirstPage(); //查询后跳转第一条

            var totalCount = 0;
            this.LoadList(out totalCount, getwhere());
            //设置分页 查询后数据变更需要重新设置分页
            this.setPage(totalCount);
        }

        /// <summary>
        /// 获取并绑定列表
        /// </summary>
        private void LoadList(out int totalCount, string where = "")
        {
            //获取字典管理
            //筛选未审核数据
            where += where.Length > 0 ? " AND SHZT='待审核'" : " SHZT='待审核'";
            ResultModel<List<QH_ZCSH>> result = HTSHDal.GetPageList(this.ucPagerControl31.PageIndex, this.ucPagerControl31.PageSize, where);
            if (result.Code == ResultCode.Success)
            {
                totalCount = result.Count;
                this.dgv_htsh.AutoGenerateColumns = false;
                this.dgv_htsh.DataSource = result.Data;
            }
            else
            {
                totalCount = 0;
                MessShow.ShowMistake(result.Message);
            }
        }

        private string getwhere()
        {
            var zczh = txt_Name.InputText;
            var where = string.Empty;
            if (zczh.Length > 0)
            {
                where += $" ZCZH like '%{zczh}%'";
            }
            return where;
        }

        private void setPage(int count)
        {
            List<object> lstPage2 = new List<object>();
            for (int i = 0; i < count; i++)
            {
                lstPage2.Add(i);
            }
            ucPagerControl31.DataSource = lstPage2;

            //ucPagerControl21.EndPage();
        }

        private void ucPagerControl31_ShowSourceChanged(object currentSource)
        {
            if (currentSource != null)
            {
                var currindex = (Json.ToObject<int[]>(currentSource.ToJson()))[0];
                if (pageIndex != currindex)
                {
                    int count = 0;
                    LoadList(out count, getwhere());
                    pageIndex = currindex;
                }
            }
        }
        private void div_htsh_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var column = this.dgv_htsh.Columns[e.ColumnIndex].DataPropertyName;
            if (column == "SHTG" || column == "SHBTG")
            {
                var msg = "是否确认通过";
                var shzt = "通过";
                if (column == "SHBTG")
                {
                    shzt = "未通过";
                    msg = "是否确认不通过";
                }
                DialogResult dialog = MessShow.ShowQuery(msg);
                if (dialog == DialogResult.OK)
                {
                    var result = HTSHDal.Shenhe(this.dgv_htsh.Rows[e.RowIndex].Cells[0].Value.ToString(), shzt);
                    if (result.Code == ResultCode.Success)
                    {
                        LoadList(out int count, getwhere());
                        MessShow.ShowSure("审核成功");
                    }
                    else
                    {
                        MessShow.ShowHint(result.Message);
                    }
                }
            }
        }
    }
}
